#include "CMainMenuState.h"
#include "COptionsState.h"
#include "CGamePlayState.h"
#include "CCreditsState.h"
#include "CGame.h"
#include "SGD Wrappers\CSGD_TextureManager.h"
#include "SGD Wrappers\CSGD_Direct3D.h"
#include "SGD Wrappers\CSGD_DirectInput.h"
#include "CBitmapFont.h"

#include "CSkillsUpgradeState.h"

CMainMenuState::CMainMenuState(void)
{

}
CMainMenuState::~CMainMenuState()
{

}
CMainMenuState* CMainMenuState::GetInstance(void)
{
	//	Lazy instantiation
	static CMainMenuState instance;
	return &instance;
}
void CMainMenuState::Enter(void)
{
	m_nMenuSelection = 0;
	m_nCursorID = CSGD_TextureManager::GetInstance()->LoadTexture( "resources//graphics//SGD_MenuCursor.png" );
	m_nBGM = CSGD_FModManager::GetInstance()->LoadSound("resources//music//StS_01_prologue_-_cirrus.OGG", FMOD_LOOP_NORMAL);
	//m_nBGID = CSGD_TextureManager::GetInstance()->LoadTexture( "resource//graphics//SGD_Menu.png" );
	
	CSGD_FModManager::GetInstance()->SetVolume(m_nBGM,CGame::GetInstance()->GetMusicVolume());
	CSGD_FModManager::GetInstance()->SetPan(m_nBGM,CGame::GetInstance()->GetPanVolume());
	float volume = CGame::GetInstance()->GetMusicVolume();

	CSGD_FModManager::GetInstance()->PlaySound(m_nBGM);
}
bool CMainMenuState::Input(void)
{

	if(CSGD_DirectInput::GetInstance()->KeyPressed(DIK_UP) )
	{
		m_nMenuSelection -= 1;
		if( m_nMenuSelection == -1 )
		{
			m_nMenuSelection = MAINMENU_EXIT;
		}

	}

	if(CSGD_DirectInput::GetInstance()->KeyPressed(DIK_DOWN) )
	{
		m_nMenuSelection += 1;

		if( m_nMenuSelection == NUM_MAINMENU_OPTIONS )
		{
			m_nMenuSelection = 0;
		}
	}

	if( CSGD_DirectInput::GetInstance()->KeyPressed(DIK_RETURN) )
	{
		switch( m_nMenuSelection )
		{
		case MAINMENU_NEWGAME:
			{
				CGame::GetInstance()->ChangeState( CGamePlayState::GetInstance() );
			}
			break;

		case MAINMENU_LOAD:	// GOES TO THE SKILLS TEST FOR NOW
			{
				CGame::GetInstance()->ChangeState( CSkillsUpgradeState::GetInstance() );
			}
			break;

		case MAINMENU_OPTIONS:
			{
				CGame::GetInstance()->ChangeState( COptionsState::GetInstance() );
			}
			break;		

		case MAINMENU_CREDITS:
			{
				CGame::GetInstance()->ChangeState( CCreditsState::GetInstance() );
			}
			break;


		case MAINMENU_EXIT:
			{
				return false;
			}
			break;

		}
	}
	return true;
}
void CMainMenuState::Update(float fElapsedTime)
{
	if( !CSGD_FModManager::GetInstance()->IsSoundPlaying(m_nBGM) )
		CSGD_FModManager::GetInstance()->PlaySound(m_nBGM);
}
void CMainMenuState::Render(void)
{
	//CSGD_TextureManager::GetInstance()->Draw(m_nBGID,0,0);


	CSGD_Direct3D::GetInstance()->GetSprite()->Flush();

	CBitmapFont::GetInstance()->SetScale(1.0f);
	RECT rTitle = {0, 0, CGame::GetInstance()->GetPlayW(), 120};
	RECT rBody = {125, 200, CGame::GetInstance()->GetPlayW(), 400};
	
	CBitmapFont::GetInstance()->PrintInRect("Spell Slingers", &rTitle, ALIGN_CENTER, D3DCOLOR_XRGB(0, 0, 255));
	CBitmapFont::GetInstance()->PrintInRect("New Game\nLoad\nOptions\nCredits\nExit", &rBody, ALIGN_LEFT, D3DCOLOR_XRGB(225, 225, 225));

	/*BF.PrintText("SPELL SLINGERS", 75, 10, 1.25f, D3DCOLOR_XRGB(255, 255, 255), true);

	BF.PrintText("PLAY", 125, 125, 1.25f, D3DCOLOR_XRGB(255, 255, 255));
	BF.PrintText("OPTIONS", 125, 165, 1.25f, D3DCOLOR_XRGB(255, 255, 255));
	BF.PrintText("SKILLS TEST", 125, 205, 1.25f, D3DCOLOR_XRGB(255, 255, 255));
	BF.PrintText("", 125, 245, 1.25f, D3DCOLOR_XRGB(255, 255, 255));
	BF.PrintText("CREDITS", 125, 285, 1.25f, D3DCOLOR_XRGB(255, 255, 255));
	BF.PrintText("EXIT", 125, 325, 1.25f, D3DCOLOR_XRGB(255, 255, 255));*/
	
	//draw the cursor at the current selection
	int topSelection = 175;
	int spacing = 63;
	switch(m_nMenuSelection)
	{
	case MAINMENU_NEWGAME:
		{
			CSGD_TextureManager::GetInstance()->Draw(m_nCursorID, 100, topSelection  + (spacing * MAINMENU_NEWGAME) );
		}
		break;

	case MAINMENU_LOAD: // skills test for now
		{
			CSGD_TextureManager::GetInstance()->Draw(m_nCursorID, 100, topSelection  + (spacing * MAINMENU_LOAD) );
		}
		break;

	case MAINMENU_OPTIONS:
		{
			CSGD_TextureManager::GetInstance()->Draw(m_nCursorID, 100, topSelection  + (spacing * MAINMENU_OPTIONS) );
		}
		break;	

	case MAINMENU_CREDITS:
		{
			CSGD_TextureManager::GetInstance()->Draw(m_nCursorID, 100, topSelection  + (spacing * MAINMENU_CREDITS) );
		}
		break;

	case MAINMENU_EXIT:
		{
			CSGD_TextureManager::GetInstance()->Draw(m_nCursorID, 100, topSelection  + (spacing * MAINMENU_EXIT) );
		}
		break;
	}

	
}
void CMainMenuState::Exit(void)
{
	//TODO:

	CSGD_FModManager::GetInstance()->StopSound(m_nBGM);
	
	CSGD_FModManager::GetInstance()->UnloadSound(m_nBGM);
	CSGD_TextureManager::GetInstance()->UnloadTexture( m_nCursorID );
}